// 修改ajax请求默认配置，在请求头中默认添加Authorization，做token权限认证
api.ajax.defaults.headers.common['Authorization'] = Cookies.get('token');

api.userOwnList()
    .then(list => {
        // 过滤菜单获取菜单列表
        let menuList = filterMenu(list.data.result)
        // 遍历菜单列表获取菜单结构
        menuList.forEach(item => {
            let liList = ''
            // 获得二级菜单结构
            let childeList = item.children.forEach(child => {
                let li =
                      '<li>'
                    +   '<a href=' + child.url + '>' + child.title + '</a>'
                    + '</li>'
                liList += li
            })
            // 获得主菜单结构
            let menuSection =
                  '<li>'
                +   '<a>'
                +       '<i class="fa fa-bug"></i>' + item.title
                +       '<span class="fa fa-chevron-down"></span>'
                +   '</a>'
                +   '<ul class="nav child_menu">'
                +       liList
                +   '</ul>'
                + '</li>'
            // 添加菜单
            document.getElementById('sideMenu').innerHTML += menuSection
        })
        // 初始化过渡效果
        init_sidebar()
    })
    .catch(err => {
        // 若请求发生错误就返回登录界面
        location.href = './login.html'
        console.log(err)
    })
/**
 * 将获取到的菜单转换为需要的菜单格式
 */
function filterMenu(beforeMenuList) {
    const MenuList = localStorage.getItem('MenuList')
    let afterMenuList = []
    beforeMenuList.forEach((item) => {
        // 获取菜单层级
        let pp = item.path.split('-')
        if (pp.length === 1) {
            let tempObj = {
                path: `/${item.path}`,
                icon: item.icon,
                name: pp[pp.length - 1],
                title: item.name,
                url: item.url,
                children: []
            }
            afterMenuList.push(tempObj)
        } else if (pp.length > 1) {
            afterMenuList.forEach(MeLi => {
                let ppChild = MeLi.path.split('/')[1].split('-')
                if (pp[pp.length - 2] === ppChild[ppChild.length - 1]) {
                    let tempObj = {
                        path: item.path,
                        icon: item.icon,
                        name: pp[pp.length - 1],
                        title: item.name,
                        url: item.url,
                        children: []
                    }
                    MeLi.children.push(tempObj)
                } else {
                    if (MeLi.children) {
                        // 循环遍历所有组件
                        mapChild(MeLi.children, item, pp)
                    }
                }
            })
        }
    })

    function mapChild(arrList, item, pp) {
        arrList.forEach(MeLi => {
            let ppChild = MeLi.path.split('-')
            if (pp[pp.length - 2] === ppChild[ppChild.length - 1]) {
                let tempObj = {
                    path: item.path,
                    icon: item.icon,
                    name: pp[pp.length - 1],
                    title: item.name,
                    url: item.url,
                    children: []
                }
                MeLi.children.push(tempObj)
            } else {
                if (MeLi.children) {
                    mapChild(MeLi.children, item, pp)
                }
            }
        })
    }
    return afterMenuList
}

/**
 * 退出当前登录
 * 2018-01-11 16:55:39
 */
function uLoginOut() {
    api.loginOut()
        .then(loginOut => {
            location.href = "./login.html";
        })
        .catch(error => {
            location.href = "./login.html"
        })
}
